package com.hujf.hdfs;

import com.sun.deploy.util.StringUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.Collections;

/**
 * @author Hujf
 * @title: HdfsClient
 * @date 2021/3/2916:09
 * @description: 常用套路
 * 1.获取客户端对象
 * 2.执行相关的操作
 * 3.关闭资源
 * <p>
 * Hdfs zookeeper
 */
public class HdfsClient {
    private static String uristr = "hdfs://hadoop101:8020";
    private FileSystem fs;

    @Before
    public void init() throws URISyntaxException, IOException, InterruptedException {
        URI uri = new URI(uristr);
        //获取文件系统
        Configuration configuration = new Configuration();
        configuration.set("dfs.replication", "2");
        fs = FileSystem.get(uri, configuration, "root");
    }

    @After
    public void close() throws IOException {
        //关闭资源
        fs.close();
    }

    @Test
    public void testMkdir() throws Exception {
        //创建一个文件
        fs.mkdirs(new Path("/xiyou/huoyanshan"));
    }

    /**
     * 参数优先级  一次递增
     * hdfs-default.xml
     * hdfs-site.xml
     * resource下site；
     * 代码配置最高级  懂得都懂 读取配置文件嘛
     */
    @Test
    public void testPut() throws IOException {
        Path p = new Path("E:\\java\\workspace\\myself\\hadoop\\hdfsclient\\src\\main\\resources\\sunwukong.txt");
        Path p2 = new Path("hdfs://hadoop101/xiyou/huaguoshan");
        //参数1 表示删除原数据 参数2 是否允许覆盖  参数3 源数据路径  参数四 目的地路径
        fs.copyFromLocalFile(false, true, p, p2);
    }

    /**
     * 文件下载
     * useRawLocalFileSystem  是否开启校验  不用就完事了
     */
    @Test
    public void testGet() throws IOException {
        Path p = new Path("E:\\java\\workspace\\myself\\hadoop\\hdfsclient\\src\\main\\resources");
        Path p2 = new Path("hdfs://hadoop101/xiyou/huaguoshan");
        fs.copyToLocalFile(false, p2, p, true);
    }


    /**
     * 文件删除
     * <p>
     * recursive   是否递归删除  用于删除非空文件夹文件夹
     */
    @Test
    public void testDel() throws IOException {
        Path p2 = new Path("hdfs://hadoop101/jinguo");
        fs.delete(p2, true);
    }

    /**
     *重命名
     *
     */
    @Test
    public void testRename() throws IOException {
       // fs.rename(new Path("/input/word.txt"),new Path("/input/hujf.txt"));
        fs.rename(new Path("/input3"),new Path("/output3"));
    }

    //获取文件详细信息
    @Test
    public void getFileDel() throws IOException {
        String []a = new String[]{"1","2"};
        String  s = StringUtils.join(Arrays.asList(a), "\t");
        System.out.println(s);
        RemoteIterator<LocatedFileStatus> iterator = fs.listFiles(new Path("/sanguo"), true);
        while(iterator.hasNext()){
            LocatedFileStatus status =iterator.next();
            System.out.println(status.getPath()+"==============================================================================");
            System.out.println("status.getPermission() = " + status.getPermission());
            System.out.println("status.getOwner() = " + status.getOwner());
            System.out.println("status.getGroup() = " + status.getGroup());
            System.out.println("status.getLen() = " + status.getLen());
            System.out.println("status.getModificationTime() = " + status.getModificationTime());
            System.out.println("status.getReplication() = " + status.getReplication());
            System.out.println("status.getBlockSize() = " + status.getBlockSize());
            System.out.println(status.getPath().getName());

            BlockLocation [] blockLocations = status.getBlockLocations();
            System.out.println("Arrays.toString(blockLocations) = " + Arrays.toString(blockLocations));
        }
    }


    @Test
    public void isfile() throws IOException {
        FileStatus[] fileStatuses = fs.listStatus(new Path("/sanguo"));
        for(FileStatus status:fileStatuses){
            if(status.isFile()){
                System.out.println("这是一个文件"+status.getPath().getName());
            }else{
                System.out.println( "这是一个文件夹" + status.getPath().getName());
            }
        }
    }
}
